home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pcmagwin.zip / CPUTYPE.C < prev    next >
Text File  |  1992-12-16  |  3KB  |  100 lines

  1. // CpuType - System Registry Demonstration for Windows NT
  2. // Retrieves and Displays CPU Type of Host Machine
  3. // Tested with Oct 92 (beta) Release of Windows NT
  4. // Copyright (C) 1992 Ray Duncan
  5. // PC Magazine * Ziff Davis Publishing
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <windows.h>
  10.  
  11. // 
  12. // GetSZKeyData() -- given a currently open registry key, the name 
  13. // of a subkey, and a value name, returns a pointer to the ASCIIZ data 
  14. // associated with that value name.  If any necessary function fails 
  15. // or the data is not ASCIIZ, returns NULL.
  16. //
  17. LPSTR GetSZKeyData(HKEY hKey, LPSTR szSubKeyName, LPSTR szValueName)
  18. {
  19.     HKEY hNewKey;
  20.     char chClass[10];
  21.     DWORD dwType;
  22.     DWORD cSubKeys;    
  23.     DWORD cbMaxSubkey; 
  24.     DWORD cbMaxClass;  
  25.     DWORD cValues; 
  26.     DWORD cbMaxValueName;
  27.     DWORD cbMaxValueData;  
  28.     DWORD cbSecurityDescriptor;    
  29.     DWORD cbClassSize = 10 ; // sizeof(chClass);
  30.     LPSTR pKeyValue;
  31.     FILETIME ftLastWriteTime;
  32.  
  33.     // open the specified subkey
  34.     if(ERROR_SUCCESS != 
  35.        RegOpenKeyEx(hKey, szSubKeyName, 0, KEY_QUERY_VALUE, &hNewKey))
  36.         return(NULL);
  37.  
  38.     // retrieve size of largest data field for this subkey
  39.     if(ERROR_SUCCESS != 
  40.        RegQueryInfoKey(hNewKey, chClass, &cbClassSize, NULL,
  41.             &cSubKeys, &cbMaxSubkey, &cbMaxClass, &cValues, &cbMaxValueName, 
  42.             &cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime))
  43.     {
  44.         RegCloseKey(hNewKey);
  45.         return(NULL);
  46.     }
  47.  
  48.     cbMaxValueData++;
  49.  
  50.     // allocate memory to hold the incoming data
  51.     pKeyValue = malloc(cbMaxValueData * sizeof(TCHAR));
  52.  
  53.     if(pKeyValue == NULL)
  54.     {
  55.         RegCloseKey(hNewKey);
  56.         return(NULL);
  57.     }
  58.  
  59.     // retrieve data for specified key, subkey, and valuename
  60.     if(ERROR_SUCCESS != 
  61.        RegQueryValueEx(hNewKey, szValueName, NULL, &dwType, 
  62.             (LPBYTE) pKeyValue, &cbMaxValueData))
  63.     {
  64.         free(pKeyValue);
  65.         RegCloseKey(hNewKey);
  66.         return(NULL);
  67.     }
  68.  
  69.     // if data is not ASCIIZ, return error
  70.     if(dwType != REG_SZ)
  71.     {
  72.         free(pKeyValue);
  73.         RegCloseKey(hNewKey);
  74.         return(NULL);
  75.     }
  76.  
  77.     // release the subkey
  78.     RegCloseKey(hNewKey);
  79.  
  80.     // return pointer to the ASCIIZ key value
  81.     return(pKeyValue);                      
  82. }                                           
  83.  
  84. // this simple demonstration of use of the registry
  85. // just retrieves and displays the CPU type.
  86. VOID main(VOID)
  87. {
  88.     LPSTR pKeyValue;
  89.  
  90.     pKeyValue = GetSZKeyData(HKEY_LOCAL_MACHINE,
  91.         "Hardware\\Description\\System\\CentralProcessor\\0",
  92.         "Identifier");
  93.  
  94.     if(pKeyValue)
  95.         printf("\nThe CPU type is: %s.\n", pKeyValue);
  96.     else
  97.         printf("\nCPU type not found in registry.\n");
  98. }
  99.  
  100.